package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Examenxreporte;
import com.lostreseditores.sabersai.model.dto.ExamenxreporteDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class ExamenxreporteView {
    private InputText txtExamxrepgeneraln;
    private InputText txtExamxrepoactivn;
    private InputText txtExamxrepunionn;
    private InputText txtReporidn_Reportes;
    private InputText txtTipoexamidn_Tipoexamen;
    private InputText txtExamxrepoidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<ExamenxreporteDTO> data;
    private ExamenxreporteDTO selectedExamenxreporte;

    public ExamenxreporteView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            ExamenxreporteDTO examenxreporteDTO = (ExamenxreporteDTO) e.getObject();

            if (txtExamxrepgeneraln == null) {
                txtExamxrepgeneraln = new InputText();
            }

            txtExamxrepgeneraln.setValue(examenxreporteDTO.getExamxrepgeneraln());

            if (txtExamxrepoactivn == null) {
                txtExamxrepoactivn = new InputText();
            }

            txtExamxrepoactivn.setValue(examenxreporteDTO.getExamxrepoactivn());

            if (txtExamxrepunionn == null) {
                txtExamxrepunionn = new InputText();
            }

            txtExamxrepunionn.setValue(examenxreporteDTO.getExamxrepunionn());

            if (txtReporidn_Reportes == null) {
                txtReporidn_Reportes = new InputText();
            }

            txtReporidn_Reportes.setValue(examenxreporteDTO.getReporidn_Reportes());

            if (txtTipoexamidn_Tipoexamen == null) {
                txtTipoexamidn_Tipoexamen = new InputText();
            }

            txtTipoexamidn_Tipoexamen.setValue(examenxreporteDTO.getTipoexamidn_Tipoexamen());

            if (txtExamxrepoidn == null) {
                txtExamxrepoidn = new InputText();
            }

            txtExamxrepoidn.setValue(examenxreporteDTO.getExamxrepoidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtExamxrepgeneraln != null) {
            txtExamxrepgeneraln.setValue(null);
            txtExamxrepgeneraln.setDisabled(true);
        }

        if (txtExamxrepoactivn != null) {
            txtExamxrepoactivn.setValue(null);
            txtExamxrepoactivn.setDisabled(true);
        }

        if (txtExamxrepunionn != null) {
            txtExamxrepunionn.setValue(null);
            txtExamxrepunionn.setDisabled(true);
        }

        if (txtReporidn_Reportes != null) {
            txtReporidn_Reportes.setValue(null);
            txtReporidn_Reportes.setDisabled(true);
        }

        if (txtTipoexamidn_Tipoexamen != null) {
            txtTipoexamidn_Tipoexamen.setValue(null);
            txtTipoexamidn_Tipoexamen.setDisabled(true);
        }

        if (txtExamxrepoidn != null) {
            txtExamxrepoidn.setValue(null);
            txtExamxrepoidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Examenxreporte entity = null;

        try {
            Integer examxrepoidn = new Integer(txtExamxrepoidn.getValue()
                                                              .toString());
            entity = BusinessDelegatorView.getExamenxreporte(examxrepoidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtExamxrepgeneraln.setDisabled(false);
            txtExamxrepoactivn.setDisabled(false);
            txtExamxrepunionn.setDisabled(false);
            txtReporidn_Reportes.setDisabled(false);
            txtTipoexamidn_Tipoexamen.setDisabled(false);
            txtExamxrepoidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtExamxrepgeneraln.setValue(entity.getExamxrepgeneraln());
            txtExamxrepgeneraln.setDisabled(false);
            txtExamxrepoactivn.setValue(entity.getExamxrepoactivn());
            txtExamxrepoactivn.setDisabled(false);
            txtExamxrepunionn.setValue(entity.getExamxrepunionn());
            txtExamxrepunionn.setDisabled(false);
            txtReporidn_Reportes.setValue(entity.getReportes().getReporidn());
            txtReporidn_Reportes.setDisabled(false);
            txtTipoexamidn_Tipoexamen.setValue(entity.getTipoexamen()
                                                     .getTipoexamidn());
            txtTipoexamidn_Tipoexamen.setDisabled(false);
            txtExamxrepoidn.setValue(entity.getExamxrepoidn());
            txtExamxrepoidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveExamenxreporte(FacesUtils.checkInteger(
                    txtExamxrepgeneraln),
                FacesUtils.checkInteger(txtExamxrepoactivn),
                FacesUtils.checkInteger(txtExamxrepoidn),
                FacesUtils.checkInteger(txtExamxrepunionn),
                FacesUtils.checkInteger(txtReporidn_Reportes),
                FacesUtils.checkInteger(txtTipoexamidn_Tipoexamen));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteExamenxreporte(FacesUtils.checkInteger(
                    txtExamxrepoidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateExamenxreporte(FacesUtils.checkInteger(
                    txtExamxrepgeneraln),
                FacesUtils.checkInteger(txtExamxrepoactivn),
                FacesUtils.checkInteger(txtExamxrepoidn),
                FacesUtils.checkInteger(txtExamxrepunionn),
                FacesUtils.checkInteger(txtReporidn_Reportes),
                FacesUtils.checkInteger(txtTipoexamidn_Tipoexamen));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtExamxrepoidn == null) {
                txtExamxrepoidn = new InputText();
            }

            txtExamxrepoidn.setValue(selectedExamenxreporte.getExamxrepoidn());

            BusinessDelegatorView.deleteExamenxreporte(FacesUtils.checkInteger(
                    txtExamxrepoidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedExamenxreporte);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer examxrepgeneraln,
        Integer examxrepoactivn, Integer examxrepoidn, Integer examxrepunionn,
        Integer reporidn_Reportes, Integer tipoexamidn_Tipoexamen)
        throws Exception {
        try {
            BusinessDelegatorView.updateExamenxreporte(examxrepgeneraln,
                examxrepoactivn, examxrepoidn, examxrepunionn,
                reporidn_Reportes, tipoexamidn_Tipoexamen);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("ExamenxreporteView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtExamxrepgeneraln() {
        return txtExamxrepgeneraln;
    }

    public void setTxtExamxrepgeneraln(InputText txtExamxrepgeneraln) {
        this.txtExamxrepgeneraln = txtExamxrepgeneraln;
    }

    public InputText getTxtExamxrepoactivn() {
        return txtExamxrepoactivn;
    }

    public void setTxtExamxrepoactivn(InputText txtExamxrepoactivn) {
        this.txtExamxrepoactivn = txtExamxrepoactivn;
    }

    public InputText getTxtExamxrepunionn() {
        return txtExamxrepunionn;
    }

    public void setTxtExamxrepunionn(InputText txtExamxrepunionn) {
        this.txtExamxrepunionn = txtExamxrepunionn;
    }

    public InputText getTxtReporidn_Reportes() {
        return txtReporidn_Reportes;
    }

    public void setTxtReporidn_Reportes(InputText txtReporidn_Reportes) {
        this.txtReporidn_Reportes = txtReporidn_Reportes;
    }

    public InputText getTxtTipoexamidn_Tipoexamen() {
        return txtTipoexamidn_Tipoexamen;
    }

    public void setTxtTipoexamidn_Tipoexamen(
        InputText txtTipoexamidn_Tipoexamen) {
        this.txtTipoexamidn_Tipoexamen = txtTipoexamidn_Tipoexamen;
    }

    public InputText getTxtExamxrepoidn() {
        return txtExamxrepoidn;
    }

    public void setTxtExamxrepoidn(InputText txtExamxrepoidn) {
        this.txtExamxrepoidn = txtExamxrepoidn;
    }

    public List<ExamenxreporteDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataExamenxreporte();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<ExamenxreporteDTO> examenxreporteDTO) {
        this.data = examenxreporteDTO;
    }

    public ExamenxreporteDTO getSelectedExamenxreporte() {
        return selectedExamenxreporte;
    }

    public void setSelectedExamenxreporte(ExamenxreporteDTO examenxreporte) {
        this.selectedExamenxreporte = examenxreporte;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
